#!/bin/bash

LogFile=/tmp/HmcInstall.log

#-------------------------------------------------------------------------------
# Common exit point
#-------------------------------------------------------------------------------
function ExitCleanup {
    # keep the log file, but ensure a different user can overwrite it next time
    cd /
    chmod 666 $LogFile

    if [ $1 -ne 0 ]; then
        exit $1
    else
        exit 0
    fi
}

#-------------------------------------------------------------------------------
# This function does the RPM installation task.
#-------------------------------------------------------------------------------
function InstallRpm {
    if [ -f /opt/hsc/data/config/NO_UPDATE_RPMS ]; then
        x="$2"
        # Strip version then leading directory name
        f=`echo ${x%%-[0-9]*}`
        r=`echo ${f##*/}`
        for i in `cat /opt/hsc/data/config/NO_UPDATE_RPMS`; do
            if [ "$r" == "$i" ]; then
                return
            fi
        done
    fi

    CMD=`echo $1`; shift;
    RPM=`echo $1`; shift;
    OPT=`echo $*`
    if [ "$OPT" == "" ]; then
        OPT="--force --nodeps"
    fi

    # Log the test install output. The format for the "normal" RPM install
    # processing inside this script is 'rpm -i <file spec> --force --nodeps'
    if [ -f $RPM ]; then
        echo "=====================================================" >> $LogFile
        echo "***** Executing rpm -vv $CMD $RPM $OPT *****         " >> $LogFile
        echo "=====================================================" >> $LogFile
        rpm -vv $CMD $RPM $OPT >> $LogFile 2>&1
        if [ $? -ne 0 ]; then
            echo "Error installing rpm fileset named $2" >> $LogFile
            if [ "$Update" == "true" ]; then
                echo "Error installing rpm fileset named $2"
                ExitCleanup 9
            fi
        fi
    fi
}

#-------------------------------------------------------------------------------
# This function does the RPM removal task.
#-------------------------------------------------------------------------------
function EraseRpm {
    # If rpm is part of no update list then do not do anything with it
    if [ -f /opt/hsc/data/config/NO_UPDATE_RPMS ]; then
        x="$2"
        for i in `cat /opt/hsc/data/config/NO_UPDATE_RPMS`; do
            if [ "$x" == "$i" ]; then
                return
            fi
        done
    fi
    echo "=====================================================" >> $LogFile
    echo "***** Executing rpm -evv $* *****                    " >> $LogFile
    echo "=====================================================" >> $LogFile
    rpm -evv $* --nodeps --allmatches >> $LogFile 2>&1
    if [ $? -ne 0 ]; then
        echo "Error removing rpm fileset named $2" >> $LogFile
    fi
}

#-------------------------------------------------------------------------------
# Start the product install...
#-------------------------------------------------------------------------------
cd /
image=$1

if [ "$image" == "" ]; then
    echo "Please specify directory containing installable packages"
    echo "usage: installImages  <directory>"
    ExitCleanup 1
fi

# Check if directory exists
if [ ! -d $image ]; then
    echo "The directory $patchdir doesn't exist"
    echo "Please specify directory containing the installable packages."
    ExitCleanup 2
fi

if [ -f /opt/hsc/data/config/NO_UPDATE_FILES ]; then
    rm -f /tmp/saved_files.tar
    cat /opt/hsc/data/config/NO_UPDATE_FILES | \
        xargs tar -cvf /tmp/saved_files.tar
fi

PATH=$PATH:/opt/IBMJava/jre/bin:
LD_LIBRARY_PATH=/opt/hsc/lib:/opt/hsc/lib/hcmjni:/lib:/usr/lib:$LD_LIBRARY_PATH
export PATH LD_LIBRARY_PATH

if [ ! -d /opt/IBMJava ]; then
    ln -sf /opt/IBMJava2-142 /opt/IBMJava
fi

# Remove the zip file to save space
rm -f /usr/local/hsc_install.images/*.zip

VER=`/opt/hsc/bin/hsc version | grep Version | cut -d':' -f2 |awk '{print $1}'`
REL=`/opt/hsc/bin/hsc version | grep Release | cut -d':' -f2`
R=`echo $REL | cut -d'.' -f1|awk '{print $1}'`
F=`echo $REL | cut -d'.' -f2|awk '{print $1}'`

# Only allow installation on V4R5.0

if [ "$VER" == "4" ]; then
   if [[ "$R" == "5" || "$R" == "4" ]]; then

    if [ "$R" == "5" ]; then
    # Check prereq

       grep -q "MH00324:" /opt/hsc/data/version
       if [ $? -ne 0 ]; then
          echo "---- This fix (MH00364) cannot be installed on this HMC Level ---"
          echo "---- It requires HMC V4R4.0 or HMC V4R5.0 and MH00324  	      ---"
          ExitCleanup 6 
       fi
    fi
    # Save old log files
    if [ -f $LogFile ]; then
        mv -f ${LogFile}.3 ${LogFile}.4 2>/dev/null
        mv -f ${LogFile}.2 ${LogFile}.3 2>/dev/null
        mv -f ${LogFile}.1 ${LogFile}.2 2>/dev/null
        mv -f ${LogFile}   ${LogFile}.1 2>/dev/null
    fi

    echo "=========================================================" >> $LogFile
    echo "*********   Performing Update operation `date`  *********" >> $LogFile
    echo "=========================================================" >> $LogFile

    # Install the base HMC rpms if baseHMC directory exists
    
    mv $image/migrcfg /opt/hsc/bin/command/
    chmod 550 /opt/hsc/bin/command/migrcfg
    chown root.root /opt/hsc/bin/command/migrcfg
    grep -q  "MH00364:" /opt/hsc/data/version
    if [ $? -eq 0 ]; then
	   grep -v "MH00364:" /opt/hsc/data/version > /tmp/_hsc_version_
	   mv /tmp/_hsc_version_ /opt/hsc/data/version
    fi
    grep -q  "MH00364: migrcfg fix for 7310CR3 (07-27-2005)" /opt/hsc/data/version
    if [ $? -ne 0 ]; then
    	echo  "MH00364: migrcfg fix for 7310CR3 (07-27-2005)" >>/opt/hsc/data/version
    fi

    echo "=========================================================" >> $LogFile
    echo "********* Install/Update complete at `date` *********"     >> $LogFile
    echo "=========================================================" >> $LogFile
  else
    echo "---- This fix (MH00364) cannot be installed on this HMC Level ---"
    echo "---- It requires HMC V4R4.0 or HMC V4R5.0. 			---"
    ExitCleanup 6 
  fi
else
    echo "---- This fix (MH00364) cannot be installed on this HMC Level ---"
    echo "---- It requires HMC V4R4.0 or HMC V4R5.0 and MH00324.	---"
    ExitCleanup 6 
fi
ExitCleanup 0
